package com.yqy.spring.T1;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class UserTest {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String className="com.yqy.spring.User";//类名
        String propertyNameInt="age";//int类型的属性名
        String propertyNameString="name";
        Class<?> clazz=Class.forName(className);
        Object obj = clazz.newInstance();
        String setMethodInt="set"+propertyNameInt.toUpperCase().charAt(0)+propertyNameInt.substring(1);
        String setMethodString="set"+propertyNameString.toUpperCase().charAt(0)+propertyNameString.substring(1);
        Method declaredIntMethod = clazz.getDeclaredMethod(setMethodInt, int.class);
        Method declaredStringMethod = clazz.getDeclaredMethod(setMethodString, String.class);
        declaredIntMethod.invoke(obj,19);
        declaredStringMethod.invoke(obj,"张三");
        System.out.println(obj);
    }
}
